package day_2022_4_to_7.day48;

import java.util.Scanner;

/**
 * 反转部分单向链表
 * @author haomin
 * @date 2022/05/26 18:51
 **/
public class Day28_1_ReversePartialLinkedList {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        Node head = new Node(-1);
        Node cur = head;
        // 1. 得到题给的链表
        for (int i = 0; i < num; i++) {
            Node node = new Node(in.nextInt());
            cur.next = node;
            cur = cur.next;
        }

        int leftIndex = in.nextInt();
        int rightIndex = in.nextInt();

        // 2. 记录left左边第一个节点
        Node frontNode = head;
        // 3. 记录right右边第一个节点
        Node hinderNode = head;

        // 2.1
        for (int i = 0; i < leftIndex-1; i++) {
            frontNode = frontNode.next;
        }
        // 得到开始进行反转的节点
        Node firstNode = frontNode.next;

        // 3.1
        for (int i = 0; i <= rightIndex; i++) {
            hinderNode = hinderNode.next;
        }

        // 4. 反转指定部分链表
        // 连接right后面的节点
        Node newNode = hinderNode;
        int count = rightIndex - leftIndex + 1;
        while (count-- != 0){
            Node pre = new Node(firstNode.val);
            pre.next = newNode;
            newNode = pre;

            firstNode = firstNode.next;
        }
        // 连接left前面的节点
        frontNode.next = newNode;

        // 5. 打印
        head = head.next;
        while (head != null){
            System.out.print(head.val+" ");
            head = head.next;
        }
    }
}

class Node{
    int val;
    Node next;

    public Node(int val) {
        this.val = val;
    }
}